﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;

namespace UI
{
    public class CommonHelper
    {
        public static void ShowErrorMessage(string errorMessage)
        {
            MessageBox.Show(errorMessage, "出错了！ Orz", MessageBoxButton.OK);
        }

        public static List<T> GetVisualChildCollection<T>(object parent) where T : UIElement
        {
            List<T> visualCollection = new List<T>();
            GetVisualChildCollection(parent as DependencyObject, visualCollection);
            return visualCollection;
        }

        private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : UIElement
        {
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child is T)
                {
                    visualCollection.Add(child as T);
                }
                else if (child != null)
                {
                    GetVisualChildCollection(child, visualCollection);
                }
            }
        }

        public static int CalcWritableBitmapPixelValue(byte alpha, byte red, byte green, byte blue)
        {
            double alphaPreMultiplier = alpha / 255d;

            byte r = (byte)(red * alphaPreMultiplier);
            byte g = (byte)(green * alphaPreMultiplier);
            byte b = (byte)(blue * alphaPreMultiplier);

            return (alpha << 24) | (r << 16) | (g << 8) | b;
        }

        public static int GetCharCountInAscii(string s)
        {
            int result = 0;
            char[] chars = s.ToCharArray();
            for(int i = 0; i < chars.Length; i++)
            {
                int count = Encoding.UTF8.GetByteCount(chars, i, 1);
                if (count == 1)
                {
                    result++;
                }
                else
                {
                    result = result + 2;
                }
            }
            return result;
        }

        public static bool IsValidPictureType(string extension)
        { 
            extension = extension.ToLower();
            if (extension == ".jpeg" || extension == ".jpg" || extension == ".jpe" || extension == ".gif" || extension == ".png")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public class DoubleAnimationProxy : DependencyObject
    {
        public DoubleAnimationProxy(Action<double> callBackAction)
        {
            this.CallBackAction = callBackAction;
        }

        private static readonly DependencyProperty DoubleProperty = DependencyProperty.Register("Double", typeof(double), typeof(DoubleAnimationProxy), new PropertyMetadata(new PropertyChangedCallback(OnDoublePropertyChanged)));

        private static void OnDoublePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DoubleAnimationProxy proxy = d as DoubleAnimationProxy;
            if (proxy.CallBackAction != null)
            {
                proxy.CallBackAction((double)e.NewValue);
            }
        }

        public double Double
        {
            get
            {
                return (double)GetValue(DoubleProperty);
            }
            set
            {
                SetValue(DoubleProperty, value);
            }
        }

        public Action<double> CallBackAction
        {
            get;
            private set;
        }
    }

    public class TiltContainer : Grid
    { 
        
    }
}
